package com.yanqu.road.server.manger.activity.yuanbaolottery;

import com.yanqu.road.dao.impl.activity.yuanbaolottery.YuanBaoLotteryConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.yuanbaolottery.YuanBaoLotteryConfig;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YuanBaoLotteryActivityConfig {
    private ActivityInfo activityInfo;
    private int costId = 124286;
    private int roundHour = 72;
    private List<YuanBaoLotteryConfig> lotteryConfigList = new ArrayList<>();

    private int nowRound;
    private long nextRoundTime;

    public YuanBaoLotteryActivityConfig(ActivityInfo tempActivityInfo, Map<String, ActivityConfig> activityConfigMap) {
        this.activityInfo = tempActivityInfo;
        this.costId = activityConfigMap.get("YUANBAO_ITEM_ID").getIntValue();
        this.roundHour = activityConfigMap.get("YUANBAO_RESET_INTERVAL_TIME").getIntValue();

        lotteryConfigList = new YuanBaoLotteryConfigDaoImpl().getYuanBaoLotteryConfigMap(activityInfo.getActivityId());
    }

    public int getNowRound() {
        long currentTime = DateHelper.getCurrentTime();
        long activityBeginTime = activityInfo.getBeginTime() * DateHelper.SECOND_MILLIONS;
        if (currentTime < activityBeginTime || currentTime >= activityInfo.getEndTime() * DateHelper.SECOND_MILLIONS) {
            nowRound = 0;
            nextRoundTime = 0;
            return 0;
        }
        if (currentTime < nextRoundTime) {
            return nowRound;
        }
        long roundSecond = roundHour * DateHelper.HOUR_MILLIONS;
        nowRound = (int) ((currentTime - activityBeginTime) / roundSecond) + 1;
        nextRoundTime = activityBeginTime + roundSecond * (nowRound);

        return 0;
    }

    public long getNextRoundTime() {
        getNowRound();
        return nextRoundTime;
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public int getCostId() {
        return costId;
    }

    public int getRoundHour() {
        return roundHour;
    }

    public List<YuanBaoLotteryConfig> getLotteryConfigList() {
        return lotteryConfigList;
    }

    public int getActivityId() {
        return activityInfo.getActivityId();
    }

    public YuanBaoLotteryConfig getLotteryConfigByTimes(int times) {
        for (YuanBaoLotteryConfig config : lotteryConfigList) {
            if (times == config.getId()) {
                return config;
            }
        }
        return null;
    }
}
